<?php
//
//	file: inc/tree.fields.class.php
//	author: Pierre ROGE pour GAIA (gaia.fr)
//	begin: 15/02/2008
//	version: 0.0.1 - 15/02/2008
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//

if ( !defined('SYS') )
{
	die('Not allowed');
}

class sys_field_tree_parent extends sys_field
{
	var $fields;

	function __construct($api_name)
	{
		parent::__construct($api_name);
		$this->fields = false;
	}

	function __destruct()
	{
		if ( isset($this->fields) )
		{
			sys::kill($this->fields);
			unset($this->fields);
		}
		parent::__destruct();
	}

	function set($name, $type, $data)
	{
		$sys = &$GLOBALS[SYS];
		$api = &$GLOBALS[$this->api_name];

		// sub-fields
		$this->fields = array();

		// create the parent list
		$this->fields['parent'] = new sys_field_list($this->api_name);
		$this->fields['parent']->set('parent', 'list', array(
			'legend' => 'sys_field_parent',
			'onchange' => (isset($data['onchange']) ? $data['onchange'] : '') . 'ajax_parent = new sys_ajax_linked_lists(); if (!ajax_parent.invoke(\'' . $api->url($sys->ini_get('ajax', 'uri'), array('mode' => 'ajax.' . $api->mode . '.parent', SYS_U_ITEM => isset($data['parent']) && isset($data['parent']['id']) && intval($data['parent']['id']) ? intval($data['parent']['id']) : 0)) . '\', this.value, \'order\')){this.submit();}',
		) + $data['parent']);
		unset($data['parent']);

		// create the order list
		$this->fields['order'] = new sys_field_list($this->api_name);
		$this->fields['order']->set('order', 'list', array(
			'legend' => 'sys_field_order',
		) + $data['order']);
		unset($data['order']);

		parent::set($name, $type, $data);
	}

	function get_value()
	{
		return false;
	}

	function check()
	{
		if ( !$this->output )
		{
			$field_names = array_keys($this->fields);
			foreach ( $field_names as $field_name )
			{
				$this->fields[$field_name]->check();
			}
		}
		return true;
	}

	function validate()
	{
		if ( !$this->output )
		{
			$field_names = array_keys($this->fields);
			foreach ( $field_names as $field_name )
			{
				$this->fields[$field_name]->validate();
			}
		}
		return true;
	}

	function display($tpl_switch=false)
	{
		$field_names = array_keys($this->fields);
		foreach ( $field_names as $field_name )
		{
			$this->fields[$field_name]->display($tpl_switch);
		}
		return true;
	}
}

?>